package com.mall.b2c.interceptor;


import com.alibaba.fastjson.JSONObject;
import com.mall.b2c.Constants;
import com.mall.b2c.bean.StoreBean;
import com.mall.b2c.bean.StoreCityBean;
import com.mall.b2c.hessian.StoreHessian;
import com.mall.common.ipseek.IPLocation;
import com.mall.common.ipseek.IPSeeker;
import com.mall.common.util.AppContext;
import com.mall.common.util.CookieUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.regex.Pattern;


public class StoreInterceptor implements HandlerInterceptor {
	@Resource private StoreHessian storeHessian;
	@Resource private IPSeeker iPSeeker;

	@Override
	public boolean preHandle(HttpServletRequest request,
							 HttpServletResponse response, Object handler) throws Exception {
		Long storeId = null;
		Cookie cookie = AppContext.getCookie(Constants.CURR_STORE_COOKIE_KEY);//选中门店
		Pattern pattern = Pattern.compile("\\d+");
		if(cookie != null && pattern.matcher(cookie.getValue()).matches()){//有选中城市
			storeId = Long.parseLong(cookie.getValue());
		}
		StoreBean store = null;
		if(storeId == null){//无选中城市
			IPLocation ipLocation = iPSeeker.getIPLocation(AppContext.getRemoteIp());//
			String cityName = "";
			if(ipLocation != null){
				cityName = ipLocation.getCity();
			}
			store = jsonToObj(storeHessian.getDefaultStore(cityName), StoreBean.class);
			storeId = store==null? 1: store.getId();
		}else{
			store = jsonToObj(storeHessian.getStoreInfo(storeId), StoreBean.class);
		}
		request.setAttribute(Constants.CURR_STORE_ATTR_KEY, store);
		CookieUtils.addCookie(request, response, Constants.CURR_STORE_COOKIE_KEY, String.valueOf(storeId));




		//自定义门店
		/*StoreBean store = (StoreBean)AppContext.getSessionAttr(Constants.CURR_STORE_SESSION_KEY);
		StoreCityBean city = (StoreCityBean)AppContext.getSessionAttr(Constants.CURR_STORE_CITY_SESSION_KEY);;
		if(store == null || city == null){
			IPLocation ipLocation = iPSeeker.getIPLocation(AppContext.getRemoteIp());//
			String cityName = "";
			if(ipLocation != null){
				cityName = ipLocation.getCity();
			}
			String cityJson = storeHession.getDefaultCity(cityName);
			city = jsonToObj(cityJson, StoreCityBean.class);
			if(city == null || city.getStores() == null || city.getStores().size() == 0){

			}else{
				store = city.getStores().get(0);
			}
		}*/
		//AppContext.setSessionAttr(Constants.CURR_STORE_SESSION_KEY, store);
		//AppContext.setSessionAttr(Constants.CURR_STORE_CITY_SESSION_KEY, city);
//		request.setAttribute(Constants.CURR_STORE_ATTR_KEY, store);
//		request.setAttribute(Constants.CURR_STORE_CITY_ATTR_KEY, city);
//		CookieUtils.addCookie(request, response, Constants.CURR_STORE_COOKIE_INFO, store.getId() + "_" + store.getCityId());
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
						   ModelAndView modelAndView) throws Exception {
		Object store  = request.getAttribute(Constants.CURR_STORE_ATTR_KEY);
//		Object city = request.getAttribute(Constants.CURR_STORE_CITY_ATTR_KEY);
		if(store != null && modelAndView != null){ //ajax请求modelAndView为null
			modelAndView.addObject(Constants.CURR_STORE_ATTR_KEY, store);
		}
		if(/*city != null &&*/ modelAndView != null){ //ajax请求modelAndView为null
			/*modelAndView.addObject(Constants.CURR_STORE_CITY_ATTR_KEY, city);*/
			modelAndView.addObject("citys", jsonToList(storeHessian.getStoreCitys(), StoreCityBean.class));
		}
	}

	/**
	 * 将json转换成对象
	 * @param json json值
	 * @param clazz 转换的类型
	 * @return
	 */
	protected <T> T jsonToObj(String json, Class<T> clazz){
		return JSONObject.parseObject(json, clazz);
	}

	/**
	 * 将json转换成list
	 * @param json json值
	 * @param clazz 转换的类型
	 * @return
	 */
	protected <T> List<T> jsonToList(String json, Class<T> clazz){
		return JSONObject.parseArray(json, clazz);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

}
